﻿using EasyDDD.AdminModule.Application.CommandsSysMenu;
using EasyDDD.AdminModule.Application.QueriesSysMenu;
using Microsoft.AspNetCore.Http;

namespace EasyDDD.AdminModule.Host.WebAPI.Controllers
{
    /// <summary>
    /// 系统菜单管理
    /// </summary>
    /// <param name="_mediator"></param>
    /// <param name="_mapper"></param>
    [Route("api/AdminModule/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class SysMenuController(IMediator _mediator, IMapper _mapper) : ControllerBase
    {
        /// <summary>
        /// 获取用户路由
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [EndpointSummary("获取用户路由")]
        public async Task<List<Dto.SysMenuGetUserRouteResult>> GetUserRoute()
        {
            var query = new SysMenuGetUserRouteQuery()
            {
                User = User,
            };
            var list = await _mediator.Send(query, HttpContext.RequestAborted);
            return _mapper.Map<List<Dto.SysMenuGetUserRouteResult>>(list);
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        [EndpointSummary("获取菜单列表")]
        [EndpointDescription("AdminModule.SysMenu.GetList")]
        [AuthorizeClaim(ClaimTypes.MenuPermissions, "AdminModule.SysMenu.GetList")]

        public async Task<List<Dto.SysMenuGetListResult>> GetList([FromQuery] SysMenuGetListQuery query)
        {
            var list = await _mediator.Send(query, HttpContext.RequestAborted);
            return _mapper.Map<List<Dto.SysMenuGetListResult>>(list);
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        [EndpointSummary("添加菜单")]
        [EndpointDescription("AdminModule.SysMenu.Add")]
        [AuthorizeClaim(ClaimTypes.MenuPermissions, "AdminModule.SysMenu.Add")]
        public async Task<Dto.SysMenuCreateResult> Create([FromBody] SysMenuCreateCommand command)
        {
            var entity = await _mediator.Send(command, HttpContext.RequestAborted);
            return _mapper.Map<Dto.SysMenuCreateResult>(entity);
        }

        /// <summary>
        /// 更新菜单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        [EndpointSummary("更新菜单")]
        [EndpointDescription("AdminModule.SysMenu.Update")]
        [AuthorizeClaim(ClaimTypes.MenuPermissions, "AdminModule.SysMenu.Update")]
        public async Task<Dto.SysMenuUpdateResult> Update([FromBody] SysMenuUpdateCommand command)
        {
            var entity = await _mediator.Send(command, HttpContext.RequestAborted);
            return _mapper.Map<Dto.SysMenuUpdateResult>(entity);
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        [EndpointSummary("删除菜单")]
        [EndpointDescription("AdminModule.SysMenu.Delete")]
        [AuthorizeClaim(ClaimTypes.MenuPermissions, "AdminModule.SysMenu.Delete")]
        public Task Delete([FromBody] SysMenuDeleteCommand command)
        {
            return _mediator.Send(command, HttpContext.RequestAborted);
        }

    }
}
